package yur.wedding.modules.sys.service.impl;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import yur.wedding.common.utils.Constant;
import yur.wedding.common.utils.Query;

import yur.wedding.modules.sys.dao.SysDoorDao;
import yur.wedding.modules.sys.entity.SysDocumentEntity;
import yur.wedding.modules.sys.entity.SysDoorEntity;
import yur.wedding.modules.sys.entity.SysMenuEntity;
import yur.wedding.modules.sys.entity.SysRoleEntity;
import yur.wedding.modules.sys.service.SysDoorService;
import yur.wedding.modules.sys.service.SysRoleDoorService;
import yur.wedding.modules.sys.service.SysUserRoleService;
import yur.wedding.utils.PageUtils;

import javax.annotation.Resource;


@Service("sysDoorService")
public class SysDoorServiceImpl extends ServiceImpl<SysDoorDao, SysDoorEntity> implements SysDoorService {


    @Resource
    SysUserRoleService sysUserRoleService;

    @Resource
    SysRoleDoorService sysRoleDoorService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SysDoorEntity> page = this.page(
                new Query<SysDoorEntity>().getPage(params),
                new QueryWrapper<SysDoorEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 查询菜单  排除按钮
     *
     * @return
     */
    @Override
    public List<SysDoorEntity> queryNotButtonList() {


        return baseMapper.queryNotButtonList();
    }

    /**
     * 根据父类菜单查子菜单
     *
     * @return
     */
    @Override
    public List<SysDoorEntity> queryListParentId(Long doorIds) {

        return baseMapper.queryListParentI(doorIds);
    }

    /**
     * 根据不同模块查询不同菜单
     *
     * @param id
     * @param userId
     * @return
     */
    @Override
    public List<SysDoorEntity> getMenuList(Long id, Long userId) {

        //判断是否最高管理员
        if (userId == Constant.SUPER_ADMIN) {


            List<SysDoorEntity> doorEntities = baseMapper.getMenuList(id);
            return doorEntities;


        } else {
            //   用户菜单------
            //   1.查询用户角色
            //   通过角色查询  查询门户权限
            List<Long> doorlist = new ArrayList();//保存数据
            List<Long> rolelist = sysUserRoleService.queryRoleIdList(userId);
            for (Long roleid : rolelist) {

                List<Long> doorIdList = sysRoleDoorService.queryDoorIdList(roleid);

                for (Long doorid : doorIdList) {

                    doorlist.add(doorid);
                }


            }

            QueryWrapper<SysDoorEntity> wrapper = new QueryWrapper<>();

            wrapper.in("door_id", doorlist);
            wrapper.eq("parent_id",id);
            Map<String, Object> params=new HashMap<>();
            IPage<SysDoorEntity> page = this.page(
                    new Query<SysDoorEntity>().getPage(params), wrapper
            );



            return page.getRecords();


        }

    }

    /**
     * 新增文档时 目录权限
     *
     * @return
     */
    @Override
    public List<SysDoorEntity> docuMentDirecTory() {

        //查询主目录  主目录菜单为  46  门户菜单
        List<SysDoorEntity> doorlist = baseMapper.queryListParentId(46L);
        //2  递归 查询子目录菜单
        getDoorTreeList(doorlist);

        return doorlist;
    }

    /**
     * 递归查询
     *
     * @param doorlist
     * @return
     */
    private List<SysDoorEntity> getDoorTreeList(List<SysDoorEntity> doorlist) {
        List<SysDoorEntity> subDoorList = new ArrayList<SysDoorEntity>();

        //遍历上级菜单
        for (SysDoorEntity emnu : doorlist) {
            //判断菜单id  等于目录类型 Constant  静态类
            if (emnu.getType() == Constant.MenuType.CATALOG.getValue()) {

                emnu.setList(getDoorTreeList(baseMapper.queryListParentId(emnu.getDoorId())));


            }
            subDoorList.add(emnu);

        }


        return subDoorList;
    }

}
